package com.bak.tt.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by yingsy on 2021-01-26.
 */
public class DruidUtils {

    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "ying";

    private static DataSource dataSource = null;
    static {
        Properties properties = new Properties();
        properties.setProperty("url",URL);
        properties.setProperty("username",USER_NAME);
        properties.setProperty("password",PASSWORD);
        properties.setProperty("driverClassName","com.mysql.jdbc.Driver");
        properties.setProperty("password",PASSWORD);
        properties.setProperty("initialSize","5");
        properties.setProperty("maxActive","20");
        properties.setProperty("maxWait","30000");
        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public DataSource getInstance(){
        return dataSource;
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}
